package com.yc.cloudvideo.sp_store.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yc.cloudvideo.entity.File;
import com.yc.cloudvideo.entity.Store;
import com.yc.cloudvideo.entity.StoreFile;
import com.yc.cloudvideo.sp_store.mapper.FileMapper;
import com.yc.cloudvideo.sp_store.mapper.StoreFileMapper;
import com.yc.cloudvideo.sp_store.mapper.StoreMapper;
import com.yc.cloudvideo.sp_store.service.IStoreService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yc.cloudvideo.util.Utils;
import com.yc.common.bean.Result;
import com.yc.common.biz.BizException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.IOException;
import java.sql.Wrapper;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author LYYT
 * @since 2022-08-03
 */
@Service
public class StoreServiceImpl extends ServiceImpl<StoreMapper, Store> implements IStoreService {
    @Resource
    StoreMapper storeMapper;
    @Resource
    FileMapper fileMapper;
    @Resource
    StoreFileMapper storeFileMapper;

    //传入仓库id，路径和要新建的文件夹名字，进行创建文件夹操作
    @Override
    @Transactional(rollbackFor = {BizException.class,IOException.class})
    public Result newFolder(int uid,int cid,String path, String folderName) throws BizException, IOException {// 一级目录文件  二级目录一号
        //获取仓库路径
        String cPath= storeMapper.selectPathByCid(cid);
        String savePath="";
        //与path和folderName拼接
//        savePath=cPath+"/"+path+"/"+folderName;
        if(!path.equals("")){
            savePath=cPath+"/"+path+"/"+folderName;
        }else {
            savePath=cPath+"/"+folderName;
        }

        System.out.println("===========savePath  qian="+savePath);
        //将左斜杠替换成右斜杠
        savePath=savePath.replaceAll("/","\\\\");//英文
        System.out.println("===========savePath="+savePath);
        //查询数据库中是否存在该文件夹
        QueryWrapper<File> wrapper=new QueryWrapper();
        wrapper.eq("path",path);
        Long count = fileMapper.selectCount(wrapper);
        if(count!=0){
            //存在，返回
            throw new BizException("文件已存在,请更换名字!");
        }
        //不存在，向file表添加一条数据
        File file=new File(folderName,LocalDateTime.now(), (long) uid,savePath);
        fileMapper.insert(file);

        //向store_file添加一条数据
        StoreFile storeFile=new StoreFile(LocalDateTime.now(), (long) cid,file.getFid(), 0L);
        storeFileMapper.insert(storeFile);

        //放在后面，防止创建了文件夹，但是sql语句报错，

        //在磁盘中创建对于资源

        java.io.File file1 = new java.io.File(Utils.toTruePath(savePath));
        file1.mkdir();

        return new  Result(1,"文件创建成功",null);
    }

    public static void main(String[] args) throws IOException {
        String p="upload\\store\\1\\测试仓库/一级目录文件/大师傅";

    }

    //传入仓库id和仓库下的一个路径，获取该路径下的所有内容
    @Override
    public List<StoreFile> selectFiles(int cid, String filePath) {
        filePath=filePath.replaceAll("/","\\\\");
        //获取仓库路径
        String cPath = storeMapper.selectPathByCid(cid);
        //获取属于这个仓库的所有文件信息
        List<StoreFile> storeFiles = storeMapper.selectFilesByCid(cid);
        //判空
        if (storeFiles.isEmpty()) return null;
        //用于循环
        int j = storeFiles.size();

        //提取传入仓库及路径下的元素
        List<StoreFile> newFiles = new ArrayList<>();//保存

        for (int i = 0; i < j; i++) {
            //去除仓库路径  和  filePath  以及      后面的 \ 或者 /
            System.out.println("==============");
            try{
                System.out.println(storeFiles.get(i).getFile().getPath());
            }catch (NullPointerException e){
                break;
            }
            String replace = storeFiles.get(i).getFile().getPath().replace(cPath + (filePath == "" ? "" : "\\" + filePath), "");
            if (replace.startsWith("\\") || replace.startsWith("/"))
                replace = replace.substring(1);

            //判断文件路径中是否含有 / 和 \  且要去除传入路径 ，如果没有，表示在一级目录，放入newFiles中
            if (!replace.contains("\\") && !replace.contains("/") && (replace != "" && !replace.isEmpty())) {
                newFiles.add(storeFiles.get(i));
            }

        }
        return newFiles;
    }
}
